//
//  LoginViewController.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/23.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "LoginViewController.h"
#import "ForgetPassword.h"
#import "ModifyDealPassword.h"
#import "Register.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"登录"];
    [self leftButton:nil];
    
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.layer.cornerRadius = BTN_RADIUS;
    _telNumLab.text = _telNum;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    _loginPwd.text = @"";
    _loginBtn.userInteractionEnabled = NO;
    _loginBtn.backgroundColor = UNUSEABLECOLOR;
}

- (IBAction)textChanged:(id)sender {
    if (_loginPwd.text.length) {
        _loginBtn.userInteractionEnabled = YES;
        _loginBtn.backgroundColor = BLUE_COLOR;
    } else {
        _loginBtn.userInteractionEnabled = NO;
        _loginBtn.backgroundColor = UNUSEABLECOLOR;
    }
}

- (IBAction)loginClick:(id)sender {
    [self.view endEditing:YES];
    JSSHOWHUD;
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Index/logout";
    [DZNetAPI request:request success:^(id response) {
        JSHIDEHUD;
        if ([response[@"rt"] integerValue] == 1) {
            [CommTools removeAllSet];
        }
        [self getSidAndRns];
    } failure:^(NSError *error) {
        JSSHOW(@"请检查网络");
    }];
}

- (void)getSidAndRns {
    DZRequestUtil *request = [[DZRequestUtil alloc] init];
    request.requestUrl = @"Index/index";
    JSSHOWHUD;
    [DZNetAPI request:request success:^(id result) {
        JSHIDEHUD;
        DZResponse *model = [DZResponse mj_objectWithKeyValues:result];
        if (model.rt == 1) {
            DZRnsAndSid *rns = [DZRnsAndSid mj_objectWithKeyValues:model.data];
            if(rns.rns)[theUser setObject:rns.rns forKey:RNS];
            if(rns.sid)[theUser setObject:rns.sid forKey:SID];
        }
        [theUser synchronize];
        [self gotoLogin:@""];
    } failure:^(NSError *error) {
        JSSHOW(LOAD_FAIL_TIP);
    }];
}

- (void)gotoLogin:(id)object {
    DZLoginRequest *request = [[DZLoginRequest alloc] init];
    request.mobile = _telNumLab.text;
    request.pwd = _loginPwd.text;
    JSSHOWHUD;
    [DZNetAPI request:request success:^(id response) {
        NSLog(@"%@",response);
        if (response) {
            if ([response[@"rt"] integerValue] == 1) {
                JSSHOW(@"登录成功");
                [self dealWithResult:response];
            } else if ([response[@"rt"] integerValue] == 10009) {
                JSSHOW(@"登录密码错误，请重新输入");
            } else {
                JSSHOW(response[@"msg"]);
            }
        }
    } failure:^(NSError *error) {
        JSSHOW(@"请检查网络");
    }];
}

- (void)dealWithResult:(id)result {
    [theUser setObject:result[@"data"][@"uInfo"][LOGIN_TOKEN] forKey:LOGIN_TOKEN];
    [theUser setObject:[NSNumber numberWithBool:YES] forKey:ISLOGIN];
    [theUser setObject:result[@"data"][@"uInfo"][MOBILE] forKey:HOSTTEL];
    [theUser synchronize];
    if (!userValue(HAVEGESTUREPWD)) {
        [theUser setObject:[NSNumber numberWithInteger:0] forKey:HAVEGESTUREPWD];
    }
    [CommTools setUserInfo:result];
    if ([userValue(HAVEGESTUREPWD) integerValue] == 0 || [userValue(HAVEGESTUREPWD) integerValue] == 3) {
        GesturePassword *gesture = [[GesturePassword alloc] init];
        gesture.gestureType = GestureTypeSettingPassword;
        gesture.hidesBottomBarWhenPushed = YES;
        UINavigationController *nav = self.navigationController;
        [nav popViewControllerAnimated:NO];
        [nav pushViewController:gesture animated:NO];
    } else {
        [self.navigationController popToRootViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"loginSuccess" object:self];
    }
}

- (IBAction)changeUserClick:(id)sender {
    NSLog(@"vcs:%@",self.navigationController.viewControllers);
    RegisterViewController *regist = [[RegisterViewController alloc] init];
    regist.hidesBottomBarWhenPushed = YES;
    UINavigationController *nav = self.navigationController;
    [nav popViewControllerAnimated:NO];
    [nav pushViewController:regist animated:NO];
    
}

- (IBAction)forgetPwdClick:(id)sender {
    ForgetPassword *forgetPwd = [[ForgetPassword alloc] init];
    [forgetPwd setupTitleView:@"忘记登录密码"];
    forgetPwd.telStr = _telNum;
    forgetPwd.sms_type = [NSString stringWithFormat:@"%d",2];
    [self.navigationController pushViewController:forgetPwd animated:YES];
}

@end
